<template>
  <div>
    <div id="chart7"></div>
  </div>
</template>
<script>
import * as echarts from "echarts";
export default {
  data() {
    return {
      myChart: null,
    };
  },
  methods: {
    echartsFn() {
      const x = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20];
      const y2 = [1,2,2,2.5,3,3.5,4,3.5,2,4,2,3,4,2,3,4,2,3,4,4];
      let option = {
        grid: {
          top: 40,
          left: 30,
          right: "4%",
          bottom: 50,
        },
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "line",
            shadowStyle: {
              color: "rgba(81, 127, 253, 0.1)",
            },
            label: {
              label: {
                show: true,
                backgroundColor: "transparent",
              },
              shadowStyle: {
                color: "rgba(81, 127, 253, 0.1)",
              },
            },
          },
          backgroundColor: "#0B2C54", // 提示框浮层的背景颜色。
          borderRadius: 5,
          textStyle: {
            // 提示框浮层的文本样式。
            color: "#fff",
          },
          className: "my-tooltip-box",
          formatter: function (params) {
            var str = '<div class="my-tooltip">';
            params.forEach((el) => {
              if (el.seriesName !== "maxdata") {
                str += `<div><span class="name large">${el.name}</span><br/><span class="value">风险等级：${el.value}</span></div>`;
              }
            });
            str += "</div>";
            return str;
          },
        },
        xAxis: {
          nameTextStyle: {
            color: "rgba(255, 255, 255, .8)",
            fontSize: 12,
            lineHeight: -25, //标题行高
            padding: [0, 0, 0, -10],
            verticalAlign: "bottom",
          },
          name: "天数",
          data: x,
          axisLine: {
            show: true, // 隐藏X轴轴线
            lineStyle: {
              color: "rgba(255, 255, 255, .3)",
              width: 1,
            },
          },
          axisTick: {
            show: true, // 隐藏X轴刻度
            alignWithLabel: true, // 刻度线和标签对齐
            length: 5, // 刻度线的长度
            lineStyle: {
              color: "rgba(255, .3)",
              width: 1,
            },
          },
          axisLabel: {
            show: true,
            textStyle: {
              color: "rgba(255, 255, 255, .8)", // X轴文字颜色
              fontSize: 12,
            },
          },
        },
        yAxis: [
          {
            type: "value",
            name: "风险等级",
            nameTextStyle: {
              color: "rgba(255, 255, 255, .8)",
              padding: [0, 0, 10, 0],
              fontSize: 12,
            },
            max: 6, // 可设置最大值
            splitLine: {
              show: true,
              lineStyle: {
                color: "#2E5178",
                width: 1,
              },
            },
            axisTick: {
              show: false,
            },
            axisLine: {
              show: false,
            },
            axisLabel: {
              show: true,
              textStyle: {
                color: "rgba(255, 255, 255, .8)",
                fontSize: 12,
              },
            },
          },
        ],
        dataZoom: [
          //滚动条
          {
            start: 0, //默认为0
            //  end: 100-1500/31,//默认为100
            end: 100, //默认为100
            type: "slider",
            show: true,
            xAxisIndex: [0],
            handleSize: 0, //滑动条的 左右2个滑动条的大小
            height: 8, //组件高度
            // left: "6%", //左边的距离
            // right: '40%',//右边的距离
            bottom: 0, //右边的距离
            fillerColor: "#409EFF", //选中范围的填充颜色
            borderColor: "#409EFF",
            borderRadius: 5,
            moveHandleSize: 0, //移动手柄的尺寸高度
            backgroundColor: "rgba(25,56,94, 1)", //背景颜色',//两边未选中的滑动条区域的颜色
            showDataShadow: false, //是否显示数据阴影 默认auto
            showDetail: false, //即拖拽时候是否显示详细数值信息 默认true
            realtime: true, //是否实时更新
            filterMode: "filter",
            handleIcon:
              "path://M30.9,53.2C16.8,53.2,5.3,41.7,5.3,27.6S16.8,2,30.9,2C45,2,56.4,13.5,56.4,27.6S45,53.2,30.9,53.2z M30.9,3.5M36.9,35.8h-1.3z M27.8,35.8 h-1.3H27L27.8,35.8L27.8,35.8z", // 画一个圆形
            handleSize: "100%",
            handleStyle: {
              color: "rgba(64,158,255,1)",
              borderWidth: 0,
            },
            borderColor: "transparent",
          },
        ],
        series: [
          {
            name: "实时水质",
            type: "line",
            yAxisIndex: 0, // 使用的 y 轴的 index，在单个图表实例中存在多个 y轴的时候有用
            // symbol: 'circle', // 标记的图形为实心圆
            symbol: `image://${require("@/assets/images/点.png")}`, // 标记的图形为实心圆
            symbolSize: 20, // 标记的大小
            smooth: false,
            itemStyle: {
              // 折线拐点标志的样式
              color: "#409EFF",
              width: 2,
              borderColor: "#164376", // 标记点边框颜色
              borderWidth: 4, // 标记点边框宽度
            },
            areaStyle: {
              //区域填充样式
              normal: {
                //线性渐变，前4个参数分别是x0,y0,x2,y2(范围0~1);相当于图形包围盒中的百分比。如果最后一个参数是‘true’，则该四个值是绝对像素位置。
                color: new echarts.graphic.LinearGradient(
                  0,
                  0,
                  0,
                  1,
                  [
                    {
                      offset: 0,
                      color: "rgba(30, 85, 146, 0.6)",
                    },
                    {
                      offset: 1,
                      color: "rgba(14, 51, 95, 0.1)",
                    },
                  ],
                  false
                ),
                shadowColor: "rgba(30, 85, 146, 0.6)", //阴影颜色
                shadowBlur: 20, //shadowBlur设图形阴影的模糊大小。配合shadowColor,shadowOffsetX/Y, 设置图形的阴影效果。
              },
            },
            lineStyle: {
              color: "#409EFF",
              width: 2,
            },
            data: y2,
            zlevel: 11,
          },
        ],
      };

      this.myChart.setOption(option);
    },
  },
  mounted() {
    this.myChart = echarts.init(document.getElementById("chart7"));
    this.echartsFn();

    // 为窗口的resize事件绑定事件处理函数
    window.addEventListener("resize", () => {
      this.myChart.resize();
    });
  },
  beforeDestroy() {
    // this.myChart.dispose()//销毁
    this.myChart.clear(); //销毁
  },
};
</script>
<style scoped lang="scss">
#chart7 {
  width: 100%;
  height: 300px;
}
</style>
